const util = require('../../../../utils/util')
import req from '../../../../utils/req'
import moment from '../../../../utils/moment'
// pages/calendar/calendar.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        wek: ['日', '一', '二', '三', '四', '五', '六']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        console.log(util.formatTime(2022, 2, 1));
        let dayData = util.getDaysWithDayCount(util.formatTime())
        let daysone = dayData[0].wek
        for (let i = 0; i < daysone; i++) {
            dayData.unshift({})
        }
        this.setData({
            dayn: options.dayn,
            month: util.formatNumber(new Date().getMonth() + 1),
            date: util.getTime(),
            choosedate: `${moment().format('YYYY')}年${moment().format('MM')}`,
            allDay: dayData,
            curDate: moment()
        })
        this.getrefuseCalendar({
            start: util.getDaysWithDayCount(util.formatTime())[0].dateStr,
            end: dayData[dayData.length - 1].dateStr
        })
    },
    changeDate(e) {
        let id = e.currentTarget.id
        let curDate
        if (id === '1') {
            curDate = this.data.curDate.subtract(1, 'months')
        } else {
            curDate = this.data.curDate.add(1, 'months')

        }

        // let date=this
        // let dayData=util.getDaysWithDayCount(util.formatTime())
        console.log();
        this.setData({
            allDay: util.getDaysWithDayCount(curDate.startOf('month').format('YYYY-MM-DD')),
            curDate,
            choosedate: `${curDate.format('YYYY')}年${curDate.format('MM')}`
        })
        this.getrefuseCalendar({
            start: curDate.startOf('month').format('YYYY-MM-DD'),
            end: curDate.endOf('month').format('YYYY-MM-DD')
        })

    },


    getrefuseCalendar(data) {
        req.getsignhistory(data)
            .then((res) => {
                // 获取
                let allDay = this.data.allDay.map((item) => {
                    let s = res.data.filter((sitem) => {
                        if (item.dateStr == sitem.date) {
                            return sitem
                        }
                    })[0]
                    item = {
                        step: s?.step,
                        ...item
                    }


                    return item
                })
                // 获取当前是星期几
                var wek = allDay[0].wek
                if (wek != 6) {
                    for (var i = 1; i <= wek; i++) {
                        allDay.unshift([''])
                    }
                }
                this.setData({
                    allDay: allDay,
                    dayn:res.dayn
                })
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
})